from api.base import Base
from loguru import logger

class UserManagerApi(Base):

    # 构造方法
    def __init__(self):
        self.add_user_url = self.get_url('/admin/admin/create')
        self.edit_user_url = self.get_url('/admin/admin/update')
        self.delete_user_url = self.get_url('/admin/admin/delete')
        self.search_user_url = self.get_url('/admin/admin/list?page=1&limit=20&sort=add_time&order=desc')

    # 新增管理员
    def add_user(self,username,password,**kwargs):
        # 1. 定义一个请求体
        user_data = {"username":username,"password":password}

        # 2. 判断kwargs是否传入参数
        if kwargs:
            logger.info("添加管理员的可选参数为:{}".format(kwargs))
            user_data.update(kwargs)
        return self.post(self.add_user_url,user_data)

    # 修改管理员
    def edit_user(self,id,username,password,**kwargs):
        # 1. 定义个请求体
        user_data = {"id":id,"username":username,"password":password}
        # 2. 判断kwargs是否传入参数
        if kwargs:
            logger.info("修改管理员的可选参数为:{}".format(kwargs))
            user_data.update(kwargs)
        return self.post(self.edit_user_url,user_data)

    # 删除管理员
    def delete_user(self,id,username,**kwargs):
        # 1. 定义个请求体
        user_data = {"id": id, "username": username}
        # 2. 判断kwargs是否传入参数
        if kwargs:
            logger.info("删除管理员的可选参数为:{}".format(kwargs))
            user_data.update(kwargs)
        return self.post(self.delete_user_url, user_data)

    # 查询管理员
    def search_user(self):
        return self.get(self.search_user_url)


if __name__ == '__main__':
    user = UserManagerApi()
    user.add_user('test01','test01')